<template>
  <div class="merchant-container">
    <el-container>
      <el-header class="header">
        <div class="logo">商家管理系统</div>
        <el-menu
          :default-active="activeMenu"
          class="el-menu-demo"
          mode="horizontal"
          router
        >
          <el-menu-item index="/merchant/add">新增商品</el-menu-item>
          <el-menu-item index="/merchant/pending">等待审核</el-menu-item>
          <el-menu-item index="/merchant/approved">审核通过</el-menu-item>
          <el-menu-item index="/merchant/rejected">审核失败</el-menu-item>
        </el-menu>
        <div class="user-info">
          <el-dropdown>
            <span class="el-dropdown-link">
              <el-avatar :size="40" :src="userStore.userImg || ''" />
              <span class="username">{{ userStore.userNickname || '商家' }}</span>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </el-header>
      <el-main>
        <router-view />
      </el-main>
    </el-container>
  </div>
</template>

<script setup lang="ts">
import { computed } from 'vue'
import { useRoute } from 'vue-router'
import { useUserStore } from '@/stores/userStore'

const route = useRoute()
const userStore = useUserStore()

const activeMenu = computed(() => route.path)

const handleLogout = () => {
  userStore.logout()
  window.location.href = '/login'
}
</script>

<style scoped>
.merchant-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  padding: 0 20px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  color: #409eff;
}

.el-menu-demo {
  flex: 1;
  margin-left: 20px;
}

.user-info {
  display: flex;
  align-items: center;
}

.username {
  margin-left: 10px;
  font-size: 14px;
}

.el-dropdown-link {
  display: flex;
  align-items: center;
  cursor: pointer;
}
</style>